package com.proxy.autoclick.clicker.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.inject.Named;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.proxy.autoclick.clicker.valueobject.KeywordAddress;
import com.proxy.autoclick.clicker.valueobject.SearchResultItem;
import com.proxy.autoclick.common.util.DataSourceUtil;
import com.proxy.autoclick.common.valueobject.Proxy;

@Named
public class ClickerDao {

	public List<KeywordAddress> getKeywordAddress(String filePath) throws FileNotFoundException
	{
		List<KeywordAddress> keywordAddressList = new ArrayList<KeywordAddress>();
		Scanner scanner = new Scanner(new File(filePath));
		while(scanner.hasNext())
		{
			String line = scanner.next();
			KeywordAddress keywordAddress = new KeywordAddress(line);
			keywordAddressList.add(keywordAddress);
		}
		return keywordAddressList;
	}
	
	/*public List<Proxy> getValidProxyList() throws SQLException
	{
        try {
            QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
            List<Proxy> myProxyList = (List<Proxy>) qr.query( 
            		"select proxy from proxies where is_valided=b'1'", 
            		new BeanListHandler(Proxy.class));
            
            return myProxyList;
            
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }
	}*/
	
	public void insetSearchResultItem(SearchResultItem searchResultItem) throws SQLException
	{
		try {
            QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
            String sql = "INSERT INTO match_result(page_rank,google_server,keyword,target_link) " +
            				"values(?,?,?,?)";
        	qr.update( sql, searchResultItem.getPageRank(),
        					searchResultItem.getGoogleServer(),
        					searchResultItem.getRequireLink() + ":" + searchResultItem.getKeyword(),
        					searchResultItem.getGoogleLink()); 
            
            
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } 
	}
}
